import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { allProjects } from '#/api/clinical/project';
import { getPopupContainer } from '@vben/utils';
import dayjs from 'dayjs';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'name',
    label: '试验名称',
  },
  {
    component: 'Input',
    fieldName: 'createByName',
    label: '创建人',
  },
  {
    component: 'RangePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    fieldName: 'createTime',
    label: '创建时间',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      getPopupContainer,
      api: () => allProjects(),
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
    fieldName: 'projectId',
    label: '所属项目',
  },
];

// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// export const columns: () => VxeGridProps['columns'] = () => [
export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '临床试验名称',
    field: 'name',
  },
  {
    title: '临床试验描述',
    field: 'description',
  },
  {
    title: '试验对象',
    field: 'researchSubject',
  },
  {
    title: '负责人',
    field: 'responsiblePersonName',
  },
  {
    title: '研究周期',
    formatter: ({ row }) => {
      return `${dayjs(row.startDate).format('YYYY-MM-DD')} - ${dayjs(row.endDate).format('YYYY-MM-DD')}`;
    },
  },
  {
    title: '创建人',
    field: 'createByName',
    width: 180,
  },
  {
    title: '创建时间',
    field: 'createTime',
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '临床试验名称',
    fieldName: 'name',
    component: 'Input',
    formItemClass: 'col-span-2',
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    fieldName: 'projectId',
    label: '所属项目',
    rules: 'required',
  },
  {
    component: 'Select',
    fieldName: 'topicId',
    label: '关联课题',
    rules: 'required',
  },
  {
    label: '试验描述',
    fieldName: 'description',
    component: 'Textarea',
    formItemClass: 'col-span-2',
    rules: 'required',
  },
  {
    label: '研究对象',
    fieldName: 'researchSubject',
    component: 'Input',
    rules: 'required',
  },
  {
    label: '研究周期',
    fieldName: 'cycleDate',
    component: 'RangePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    formItemClass: 'col-span-1',
    rules: 'required',
  },
  {
    label: '参与研究人员',
    fieldName: 'researcherIds',
    component: 'Input',
    formItemClass: 'col-span-2',
    // rules: 'required',
  },
  {
    label: '负责人',
    fieldName: 'responsiblePersonId',
    component: 'Select',
    // rules: 'required',
  },
  {
    label: '试验人数',
    fieldName: 'participantCount',
    component: 'InputNumber',
    componentProps: {
      min: 0,
      addonAfter: '人',
    },
    rules: 'required',
  },
  {
    label: '是否设置分组',
    fieldName: 'hasGroups',
    component: 'RadioGroup',
    defaultValue: true,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    rules: 'required',
  },
  {
    label: '分组名称',
    fieldName: 'groupIds',
    component: 'Input',
    defaultValue: [],
    formItemClass: 'col-span-2',
    dependencies: {
      show: (values) => values.hasGroups === true,
      triggerFields: ['hasGroups'],
    },
  },
];
